package com.zhiwang.planet.mvp.p

import com.zhiwang.planet.BuildConfig
import com.zhiwang.planet.di.ActivityScope
import com.zhiwang.planet.mvp.v.RankDataView
import com.zhiwang.planet.mvp.v.RankProfitClaimView
import com.zhiwang.planet.mvvm.model.RestApi
import com.zhiwang.planet.util.androidSchedule
import com.zhiwang.planet.util.responseFunc
import com.zhiwang.planet.util.responseVoidFunc
import com.uber.autodispose.AutoDispose
import com.uber.autodispose.AutoDispose.autoDisposable
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider
import com.zhiwang.planet.util.AppHelper
import javax.inject.Inject
import javax.inject.Named

@ActivityScope
class RankPresenter @Inject constructor(var restApi: RestApi, var scopeProvider: AndroidLifecycleScopeProvider, @Named("isYesterday") var isYesterday: Boolean) {

    lateinit var rankView: RankDataView
    lateinit var rankProfitClaimView: RankProfitClaimView

    fun getRankList() {
        if (isYesterday) {
            restApi.getYesterdayRankList(baseUrl() + "jcrank/jingcai_day", "yesterday")
                    .androidSchedule()
                    .responseFunc()
                    .`as`(AutoDispose.autoDisposable(scopeProvider))
                    .subscribe({
                        rankView.onLoadRankData(it)
                    }, {
                        rankView.onError(it)
                    }, {

                    })
        } else
            restApi.getTodayRankList(baseUrl() + "jcrank/jingcai_day", "today")
                    .androidSchedule()
                    .responseFunc()
                    .`as`(AutoDispose.autoDisposable(scopeProvider))
                    .subscribe({
                        rankView.onLoadRankData(it)
                    }, {
                        rankView.onError(it)
                    }, {

                    })
    }

    fun claimRankProfit() {

        restApi.claimRankProfit(baseUrl() + "rank/get_jcrank_reward", "1")
                .androidSchedule()
                .responseVoidFunc()
                .`as`(autoDisposable(scopeProvider))
                .subscribe({

                }, {
                    rankProfitClaimView.onError(it)
                }, {
                    rankProfitClaimView.onRankProfitClaimed()
                })

    }

    fun baseUrl(): String {
        return if (AppHelper.testEnv()) BuildConfig.DEBUG_Ji7_BASE_URL else BuildConfig.Ji7_BASE_URL
    }
}